package ro.gateway.aida.locations;

import java.util.ArrayList;

import org.apache.struts.action.ActionForm;

import ro.gateway.aida.obj.AIDALocation;
import ro.gateway.aida.obj.CRegion;
import ro.gateway.aida.obj.Country;
import ro.gateway.aida.obj.County;
import ro.gateway.aida.obj.Region;
import ro.gateway.aida.obj.Town;
import ro.gateway.aida.obj.Village;


/**
 * <p>Title: Romanian AIDA</p> <p>Description: </p> <p>Copyright: Copyright (comparator) 2003</p> <p>Company:
 * ro-gateway</p>
 *
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: LocationForm.java,v 1.1 2004/10/24 23:37:41 mihaipostelnicu Exp $
 */
public class LocationForm extends ActionForm {
     String country_iso3;
     int cregion_id = -1;
     long region_id = -1;
     long county_id = -1;
     long town_id = -1;
     long village_id = -1;
     String field_changed;
     CRegion[] cregions;
     CRegion cregion;
     Country[] countries = null;
     Country country = null;
     Region[] regions = null;
     Region region = null;
     County[] counties = null;
     County county = null;
     Town[] towns = null;
     Town town = null;
     Village[] villages = null;
     Village village = null;

     // for finder
     String what = null;
     AIDALocation locations[] = null;
     int selected_index = -1;

     // action
     String action = null;

     // editor's key
     String key = null;

     // blocker for country
     String block_country;
     String block_cregion;


     public String getKey() {
          return key;
     }

     public String getBlock_country() {
          return block_country;
     }

     public void setBlock_country( String block_country ) {
          this.block_country = block_country;
     }

     public String getBlock_cregion() {
          return block_cregion;
     }

     public void setBlock_cregion( String block_cregion ) {
          this.block_cregion = block_cregion;
     }

     public void setKey( String key ) {
          this.key = key;
     }

     public String getAction() {
          return action;
     }

     public void setAction( String action ) {
          this.action = action;
     }

     public int getSelected_index() {
          return selected_index;
     }

     public void setSelected_index( int selected_index ) {
          this.selected_index = selected_index;
     }

     public String getWhat() {
          return what;
     }

     public void setWhat( String what ) {
          this.what = what;
     }

     public AIDALocation[] getLocations() {
          return locations;
     }

     public void setLocations( AIDALocation[] locations ) {
          this.locations = locations;
     }

     public Country[] getCountries() {
          return countries;
     }

     public void setCountries( Country[] countries ) {
          this.countries = countries;
     }

     public Country getCountry() {
          return country;
     }

     public void setCountry( Country country ) {
          this.country = country;
     }

     public Region[] getRegions() {
          return regions;
     }

     public void setRegions( Region[] regions ) {
          this.regions = regions;
     }

     public Region getRegion() {
          return region;
     }

     public void setRegion( Region region ) {
          this.region = region;
     }

     public County[] getCounties() {
          return counties;
     }

     public void setCounties( County[] counties ) {
          this.counties = counties;
     }

     public County getCounty() {
          return county;
     }

     public void setCounty( County county ) {
          this.county = county;
     }

     public Town[] getTowns() {
          return towns;
     }

     public void setTowns( Town[] towns ) {
          this.towns = towns;
     }

     public Town getTown() {
          return town;
     }

     public void setTown( Town town ) {
          this.town = town;
     }

     public Village[] getVillages() {
          return villages;
     }

     public void setVillages( Village[] villages ) {
          this.villages = villages;
     }

     public Village getVillage() {
          return village;
     }

     public void setVillage( Village village ) {
          this.village = village;
     }

     public String getField_changed() {
          return field_changed;
     }

     public void setField_changed( String field_changed ) {
          this.field_changed = field_changed;
     }

     public long getCounty_id() {
          return county_id;
     }

     public void setCounty_id( long county_id ) {
          this.county_id = county_id;
     }

     public long getTown_id() {
          return town_id;
     }

     public void setTown_id( long town_id ) {
          this.town_id = town_id;
     }

     public long getVillage_id() {
          return village_id;
     }

     public void setVillage_id( long village_id ) {
          this.village_id = village_id;
     }

     public String getCountry_iso3() {
          return country_iso3;
     }

     public void setCountry_iso3( String country_iso3 ) {
          this.country_iso3 = country_iso3;
     }

     public long getRegion_id() {
          return region_id;
     }

     public void setRegion_id( long region_id ) {
          this.region_id = region_id;
     }

     public int getCregion_id() {
          return cregion_id;
     }

     public void setCregion_id( int cregion_id ) {
          this.cregion_id = cregion_id;
     }

     public CRegion[] getCregions() {
          return cregions;
     }

     public void setCregions( CRegion[] cregions ) {
          this.cregions = cregions;
     }

     public CRegion getCregion() {
          return cregion;
     }

     public void setCregion( CRegion cregion ) {
          this.cregion = cregion;
     }

     public String getMarkedField() {
          String result = "cregion";
          while ( true ) {
               if ( cregion == null ) break;

               if ( country == null ) {
                    result = "country";
                    break;
               }
               if ( region == null ) {
                    result = "region";
                    break;
               }
               if ( county == null ) {
                    result = "county";
                    break;
               }
               if ( town == null ) {
                    result = "town";
                    break;
               }
               if ( village == null ) {
                    result = "village";
               }
               break;
          }

          return result;
     }

     public void clear_objects() {
          this.countries = null;
          this.country = null;
          this.regions = null;
          this.region = null;
          this.counties = null;
          this.county = null;
          this.towns = null;
          this.town = null;
          this.villages = null;
          this.village = null;
          this.cregion = null;
          this.cregions = null;
     }

     public void reset_data() {
          clear_objects();
          this.country_iso3 = null;
          this.county_id = -1;
          this.town_id = -1;
          this.village_id = -1;
          this.region_id = -1;
          this.cregion_id = -1;
          this.block_country = null;
          this.block_cregion = null;
     }

     public void filterFinderResults() {
//            System.out.println( "filtering results" );
          if ( this.block_country == null && this.block_cregion == null ) return;
          int cregion_id = -1;
          if ( this.block_cregion != null ) {
               try {
                    cregion_id = Integer.parseInt( this.block_cregion );
               } catch ( Exception ex ) {
               }
          }
          if ( locations == null ) return;

          ArrayList items = new ArrayList();
          for ( int i = 0; i < locations.length; i++ ) {
               if ( block_country != null ) {
                    if ( block_country.equals( locations[ i ].getCountry().getIso3() ) ) {
                         items.add( locations[ i ] );
                    }
               } else if ( cregion_id != -1 ) {
                    if ( cregion_id == locations[ i ].getCregion_id() ) {
                         items.add( locations[ i ] );
                    }
               }
          }

          locations = null;
          if ( items.size() > 0 ) {
               locations = new AIDALocation[ items.size() ];
               items.toArray( locations );
          }

          return;
     }
}
